package api_servers
{
	import flash.net.URLVariables;
	
	public class pjf_server extends api_externalserver {
	    private static const SERVER : String 	= pi_servers.ADDR[0] + '/games/index_pjf_es.php';
	    public static const MODEL: String 		= 'pjf01';
	    
		public function pjf_server(a_sroot:Object) {
			var a_vars: Object = a_sroot.stage.loaderInfo.parameters;
			
			var _app_id:String = a_vars["app_id"];
			
			super(a_sroot, SERVER, a_vars["viewer_id"], _app_id, secrets[_app_id]);
			vars = a_vars;
		}

		public function get secrets(): Object {
			return {
				'pj_free':'2567b2f9c525b55c8fd0b226df3d06b7'
			}
		}
		
	    override protected function prepareData(a_queryID:String, data:*):* {
	    	if ((data.length > 0) && data[data.length - 1].hasOwnProperty('errors')) {
	    		return null
	    	} else return super.prepareData(a_queryID, data);
	    }
	    
		override protected function getSig( arr: Array): String {
			var sig2: String = '';
			for( var i:uint = 0; i < arr.length; i++ ) {
				sig2 += arr[i].pn + '=' + arr[i].pv;
			}
			return sig2 + secret;
		}
		
		override protected function srHelper( arr:Array): URLVariables {// sendRequestHelper
			arr.push( {pn: "app_id", pv: app_id } );
//			if (_session_key) arr.push( {pn: "session_key", pv: _session_key } );
			arr.sortOn('pn');
			
			var url_vars:URLVariables = new URLVariables();
			for( var i:uint = 0; i < arr.length; i++ ) {
				url_vars[ (arr[i].pn) ] = arr[ i ].pv;
			}
			url_vars["sig"] = makeSig( arr );
			return url_vars;
		}
		
		public function oneQuery(method: String, str_params: String='', _handle: Function=null, a_cache: Boolean=false): void {
			var params: Array = [
				{pn:'querycount', pv: 1},
				{pn:'query1', pv: MODEL + ';' + method + ';' + str_params}
			];

			jsrHelper(params, _handle, method, a_cache);
		}
		
	    override public function getProfiles(uids: String, handler_:*, fields: String='', queryID:String=''): void {
	    	oneQuery('getProfiles', uids, handler_);
	    }

	    public function getAlbums(uids: String, handler_:*, fields: String='', queryID:String=''): void {
	    	oneQuery('getAlbums', uids, handler_);
	    }

	    public function getPhotos(uid: String , aid: String, handler_:*, fields: String='', queryID:String=''): void {
	    	oneQuery('getPhotos', aid, handler_);
	    }
	}
}